import requests
import json, execjs
from openpyxl.workbook import Workbook
from pandas.io.formats.format import return_docstring

jsCode = execjs.compile(open('emo.js', 'r', encoding='utf-8').read()).call

if __name__ == "__main__":
    url = 'https://hz.zczy56.com/oms-consignor-pc/order/source/queryBatchGoodsSource'
    page = 200
    pageSize = 100

    # 进行UA伪装：让爬虫对应的请求载体身份标识伪装成某一个浏览器
    headers = {
        "authority": "hz.zczy56.com",
        "method": "POST",
        "path": "/oms-consignor-pc/order/source/queryBatchGoodsSource",
        "scheme": "https",
        "accept": "application/json, text/plain, */*",
        "accept-encoding": "gzip, deflate, br, zstd",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "aiossotokenid": "19b2179a-454e-48a7-9a75-545eee3c1199",
        "cache-control": "no-cache",
        "content-length": "573",
        "content-type": "application/json;charset=UTF-8",
        "cookie": "acw_tc=0b32825a17267076947061366e05c05a26d60c5d511e089c0283155836e091",
        "origin": "https://hz.zczy56.com",
        "pragma": "no-cache",
        "priority": "u=1, i",
        "referer": "https://hz.zczy56.com/sourceOfGoods.html",
        "sec-ch-ua": "\"Chromium\";v=\"128\", \"Not;A=Brand\";v=\"24\", \"Microsoft Edge\";v=\"128\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0",
        "x-requested-with": "XMLHttpRequest"
    }

    data = {
        "encryParam": jsCode('Encrypt', page, pageSize)
    }
    print(data)
    # 网络请求
    # response = requests.get(url=url, params=param, headers=headers)
    data = json.dumps(data, separators=(',', ':'))
    response = requests.post(url, headers=headers, data=data)

    # 转为json数据
    list_data = response.json()
    print(list_data['code'])
    print(list_data['msg'])
    print(list_data['data'])
    print('nowPage:', list_data['data']['nowPage'], 'pageSize:', list_data['data']['pageSize'], 'totalSize:',
          list_data['data']['totalSize'], 'totalPage:', list_data['data']['totalPage'], 'totalSize:', list_data['data']['totalSize'])

    # 输出为excel
    # df = pd.DataFrame(list_data)
    # df.to_excel('wuliu.xlsx', index=False)

    # 输出为json文件
    # fp = open('./wuliu.json', 'w', encoding='utf-8')
    # json.dump(list_data, fp=fp, ensure_ascii=False)

    # 提取需要的数据
    # with open('./wuliu.json', 'r', encoding='utf-8') as f:
    #     data = json.load(f)

    if list_data['data']['totalSize'] == '0':
        print("结束!!!")

    json_data = list_data['data']['rootArray']

    wb = Workbook()
    ws = wb.active

    # 写入表头
    header = ["起点", "终点", "运程(km)", "增值服务运费", "货品", "货物备注", "装货开始时间", "装货结束时间",
              "价格(含税)"]
    ws.append(header)

    for row in json_data:
        despatch = row['despatchPro'] + row['despatchDis']
        deliver = row['deliverPro'] + row['deliverCityStr']
        distance = row['distance']

        if 'advanceRatio' in row:
            advanceRatio = row['advanceRatio']
        else:
            advanceRatio = '无'

        goodsDescription = row['goodsDescription']

        if 'cargoRemark' in row:
            cargoRemark = row['cargoRemark']
        else:
            cargoRemark = '无'

        despatchStart = row['despatchStart']
        despatchEnd = row['despatchEnd']
        if "price" in row:
            if row['freightType'] == '1':
                price = '单价:' + row['price']
            else:
                price = '包车:' + row['price']
        else:
            price = '无'

        ws.append(
            [despatch, deliver, distance, advanceRatio, goodsDescription, cargoRemark, despatchStart, despatchEnd, price])

    wb.save('./zczy.xlsx')

    print('over!')
